package com.zt.common.utils;

import cn.hutool.log.Log;
import cn.hutool.log.LogFactory;
import com.zt.common.Do.ClassWithTeachDO;
import com.zt.common.pojo.ActiveClass;
import com.zt.common.pojo.common.SemesterStatus;
import com.zt.common.pojo.common.StartClassStatus;

import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.List;

/**
 * description:
 * author:zt
 * date:2021-12-28
 */
public class ClassUtil {
    private static final Log log = LogFactory.get();

    /**
     * 根据当前日期 与 传入数值，获取 对应的一个学期
     * 获取逻辑：9-2月为上学期，3-8月为下学期
     * @param i 0表示当前学期，1表示下学期 依次递增
     * @return 学期字符串
     */
    public static String getSemester(int i) {
        String semester = "";
        int part = 0;

        int j = 0;
        if (i < 0){
            j = -i; i = 0;
        }

        Calendar calendar = Calendar.getInstance();
        //获取当前年份
        int year = calendar.get(Calendar.YEAR);
        //获取当前月份
        int month = calendar.get(Calendar.MONTH) + 1;

        if (month>=3 && month<=8){
            if (i==0){
                //直接返回当前学期
                semester = (year-1) + "-" + (year) + "-" + 2;
            } else if (i%2==0){
                //i是偶数
                semester = (year-1+(i/2)) + "-" + (year+(i/2)) + "-" + 2;
            }else{
                //i是奇数
                semester = (year+(i/2)) + "-" + (year+1+(i/2)) + "-" + 1;
            }
        } else if (month>=9 && month<=12){
            part = (i%2==0)? 1:2;
            semester = (year+(i/2)) + "-" + (year+(i/2)+1) + "-" + part;
        }
        else if (month<=2){
            part = (i%2==0)? 1:2;
            semester = (year+(i/2)-1) + "-" + (year+(i/2)) + "-" + part;
        }

        //表示传进来的i是负数，获取之前的学期
        //2021-2022-2
        if (j != 0){
            String[] split = semester.split("-");
            String first;
            if (split[2].equals("2")) {
                first = String.valueOf(Integer.parseInt(split[0]) - (j / 2));
                semester = first + "-" + (Integer.parseInt(first) + 1) + "-" + ((j%2==0)?2:1);
            } else {
                first = String.valueOf(Integer.parseInt(split[0]) - (j+1)/2);
                semester = first + "-" + (Integer.parseInt(first) + 1) + "-" + ((j%2==0)?1:2);
            }
        }

        return semester;
    }

    /**
     * 判断未来n个学期管理员是否已经开课选课
     * @return 获取未来n个学期的开课状态
     */
    public static ArrayList<SemesterStatus> getSemesterStatus(int n) {
        RedisUtil redisUtil = ApplicationContextUtil.get(RedisUtil.class);
        ArrayList<SemesterStatus> list = new ArrayList<>();
        //获取过往学期
        boolean past = false;
        if (n < 0) {
            n = -n; past = true;
        }
        for (int i = 0; i < n; i++) {
            String semester = ClassUtil.getSemester(past?-i:i);
            log.info("获取" + semester + "开课选课状态");

            short hasSelectClass = StartClassStatus.NOT_START.getValue(), hasStartClass = StartClassStatus.NOT_START.getValue();
            String expireTime = "";

            //如果redis中没有 该学期作为key的值，那么表示该学期还未开始开课/选课
            if (redisUtil.hasKey(semester + "-hasStartClass")) {
                //若redis中有该学期作为key的值，则判断该key下有无 开课/选课的key
                expireTime = (String) redisUtil.get(semester + "-hasStartClass");
                if (redisUtil.hasKey(semester + "-startClass")) {
                    hasStartClass = StartClassStatus.HAS_STARTED.getValue();
                }
                else {
                    hasStartClass = StartClassStatus.HAS_OVER.getValue();
                }
            }
            if (redisUtil.hasKey(semester + "-hasSelectClass")) {

                expireTime = (String) redisUtil.get(semester + "-hasSelectClass");
                if (redisUtil.hasKey(semester + "-selectClass")){
                    hasSelectClass = StartClassStatus.HAS_STARTED.getValue();
                }
                else {
                    hasSelectClass = StartClassStatus.HAS_OVER.getValue();
                }
            }
            list.add(new SemesterStatus(semester,hasStartClass,hasSelectClass,expireTime));
        }

        return list;
    }

    /**
     * @param classes activeClass数组
     * @return 数组中所有activeClass 的上课时间
     */
    public static String getClassTime(List<ActiveClass> classes){
        //105 = 7 * 15
        int[] time = new int[105];
        for (ActiveClass activeClass : classes) {
            //将上课时间 字符串转为int数组
            int[] classTime = Arrays.stream(activeClass.getClassTime().split("")).mapToInt(Integer::parseInt).toArray();
            //相或 得1
            for (int i = 0; i < time.length; i++) {
                time[i] |= classTime[i];
            }
        }
        StringBuilder sb = new StringBuilder();
        for (int i : time) sb.append(i);

        return sb.toString();
    }

    public static String getClassTime2(List<ClassWithTeachDO> classes){
        //105 = 7 * 15
        int[] time = new int[105];
        for (ClassWithTeachDO activeClass : classes) {
            //将上课时间 字符串转为int数组
            int[] classTime = Arrays.stream(activeClass.getClassTime().split("")).mapToInt(Integer::parseInt).toArray();
            //相或 得1
            for (int i = 0; i < time.length; i++) {
                time[i] |= classTime[i];
            }
        }
        StringBuilder sb = new StringBuilder();
        for (int i : time) sb.append(i);

        return sb.toString();
    }

    /**
     * @param time1 time2 7*15上课时间
     * @return 返回两个上课时间是否冲突
     */
    public static boolean isTimeConflict(String time1, String time2){
        //学校班级上课时间
        int[] classTime1 = Arrays.stream(time1.split("")).mapToInt(Integer::parseInt).toArray();
        //当前要开设班级上课时间
        int[] classTime2 = Arrays.stream(time2.split("")).mapToInt(Integer::parseInt).toArray();

        for (int i = 0; i < classTime1.length; i++) {
            //同时为1 时表示冲突
            classTime1[i] &= classTime2[i];
            if (classTime1[i] == 1) return true;
        }
        return false;
    }

    /**
     * @param classId 班级id
     * @return 根据班级id获取该班即的所有资源名字
     */
    public static String[] getClassResources(int classId) throws NullPointerException{
        File file = new File("/root/resources/" + classId + "/");

        log.info("获取classId：" + classId + "的资源名");
        return file.list();
    }
}
